<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Session;
use App\Models\BlastUserModel;

class LoginController extends Controller
{
    public function login(){
    	return view('admin.login.login');
    }

    public function check(Request $request){
    	$username = $request->input('username');
    	$password = $request->input('password');

    	$info = BlastUserModel::checkUsername($username,$password);
        
    	if($info['status'] === true){
    		// 使用session保存登录状态
    		Session::put('isLogin','yes');
            Session::put('username',$info['name']);
    		return redirect('/admin/home');
    	}else{
    		// 使用一次性session返回错误信息
    		Session::flash('message','账号密码错误');
    		return redirect('/admin/login');
    	}
    }

    public function logOut(){
    	// 删除所有的登录信息和状态
    	Session::forget('isLogin');
        Session::forget('username');
    	return redirect('/admin/');
    }

    // 临时生成后台登录账户方法
    /*public function add_user(){
    	date_default_timezone_set('PRC');
    	$user = new BlastUserModel;
        // dd($user);
    	$user->username = 'admin';
        $user->token = substr(md5('Admin@123!'),0,8);
        $user->name = '系统管理员';
        $user->check='normal';
        $user->password = md5($user->token.md5('Admin@123!'));
        $user->add_date = date('Y-m-d H:i:s');

    	$user->save();
    	dd($user->id);
    }*/
}
